
//生成APP申请信息表
$("#showAPPApplyInfo").click(function () {
    $.ajax({
        type: "post",
        url: "apply/getInfo",
        data: {},
        success: function (data) {
            $("#applyTable").empty();
            //往tbody里面创建行，有几个人（通过数组的长度）我们就创建几行
            var tbody=document.getElementById("applyTable");
            for(var i=0;i<data.length;i++){
                var tr=document.createElement("tr");
                tbody.appendChild(tr);
                var td2=document.createElement("td");
                td2.innerHTML=i+1;
                tr.appendChild(td2);
                var td3=document.createElement("td");
                td3.innerHTML=data[i].issuer;
                tr.appendChild(td3);
                var td4=document.createElement("td");
                td4.innerHTML=data[i].appname;
                tr.appendChild(td4);
                var td5=document.createElement("td");
                td5.innerHTML=data[i].issuetime;
                tr.appendChild(td5);
                var td7=document.createElement("td");
                td7.innerHTML=data[i].function;
                tr.appendChild(td7);
                var td8=document.createElement("td");
                td8.innerHTML=data[i].displaypath;
                tr.appendChild(td8);
                var td9=document.createElement("td");
                td9.innerHTML=data[i].issueadress;
                tr.appendChild(td9);
                var td10=document.createElement("td");
                td10.innerHTML=data[i].email;
                tr.appendChild(td10);
                var td11=document.createElement("td");
                td11.innerHTML=data[i].phonenumber;
                tr.appendChild(td11);
                var td12=document.createElement("td");
                var states=data[i].state;
                var re;
                if(states=="0"){
                    re="正在审核";
                }else if(states=="1"){
                    re="审核通过";
                }else{
                    re="审核不通过";
                }
                td12.innerHTML=re;
                tr.appendChild(td12);
            }

        }
    });
})

function actionFormatter(value, row, index) {
    return [
        '<a class="edit ml10" href="javascript:void(0)" title="Edit">',
        '<i class="glyphicon glyphicon-edit"></i> 编辑',
        '</a>'
    ].join('');
}


function roleShowFormatter(value, row, index) {
    return [
        '<a class="edit ml10" href="javascript:void(0)" title="Edit">',
        '<i class="glyphicon glyphicon-check"></i> 查看',
        '</a>'
    ].join('');
}

//修改申请状态
function addItem(event) {
    var checkState="正在审核";
    if($('input[name="optionsRadios1"]:checked').val() == 1){
        checkState=1;
    }else if($('input[name="optionsRadios1"]:checked').val() == 3){
        checkState=0;
    }else{
        checkState=2;
    }
    var rowDate={

        issuer:$("#issuer").val(),
        appname:$("#appname").val(),
        issuetime:$("#issuetime").val(),
        function:$("#function").val(),
        displaypath:$("#displaypath").val(),
        issueadress:$("#issueadress").val(),
        email:$("#email").val(),
        phonenumber:$("#phonenumber").val(),
        state:checkState
    };
    rowDate["id"] = $('#problemsTable').bootstrapTable('getData')[event.data].id;
    judgeState(rowDate);
    $.ajax({
        type: "post",
        url: "apply/editState",
        data: rowDate,
        success: function () {
            $("#appCheck").modal('hide');
            getAllAPPInfo();
            tomcatProjectInfo=[];
            addAllApp();
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {//输出错误信息
            alert('审核失败，请稍后重试');
        }
    });
}

//判断当APP审核状态改变时，侧边栏的额分类内容是否需要改变
function judgeState(datas) {
    var id=datas.id;
    var state=datas.state;
    for(var i=0;i<tomcatProjectInfo.length;i++){
        var temId=tomcatProjectInfo[i].id;
        if (temId===id){
            if (state!==1) {
                $.ajax({
                    type: "post",
                    url: "category/editCategory",
                    data: datas,
                    success: function () {

                    }
                });
            }

        }
    }
}


//删除APP申请信息
function deleteAPPInfo() {
    //遍历数组中的每个元素，并按照return中的计算方式 形成一个新的元素，放入返回的数组中
    var ids = $.map($('#problemsTable').bootstrapTable('getSelections'), function (row) {
        return row.id;
    });
    if (ids.length==0){
        alert("请先做出选择，再删除！");
    }else{
        if (confirm("是否确认删除选中的APP信息?")){

            $.ajax({
                type: "post",
                url: "apply/deleteAPPInfo",
                data: {"ids":ids},
                success: function () {

                    getAllAPPInfo();
                    tomcatProjectInfo=[];
                    addAllApp();

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {//输出错误信息
                    alert('删除失败，请稍后重试');
                }
            });
        }
    }


}

//管理员页面APP审核按钮触发的事件
window.actionEvents ={
    'click .edit': function (e, value, row, index) {
        $("#appCheck").modal('show');
        $("#problemBtn").css("display","inline");

        if(row.states=="审核通过"){
            $('input[name="optionsRadios1"]')[0].checked = true;
        }else if(row.states=="正在审核"){
            $('input[name="optionsRadios1"]')[2].checked = true;
        }else{
            $('input[name="optionsRadios1"]')[1].checked = true;
        }
        updateindex=index;
        $("#problemBtn").unbind("click");
        $("#problemBtn").click(index,addItem);

    }
};

//管理员页面加载APP信息
function getAllAPPInfo() {
    $.ajax({
        type: "post",
        url: "apply/getAllInfo",
        data: {},

        success: function (result) {
            $('#problemsTable').bootstrapTable('removeAll');
            if (result.length > 0) {
                var results=[];
                for (var i=0;i<result.length;i++) {
                    var states=result[i].state;
                    var re;
                    if(states=="0"){
                        re="正在审核";
                    }else if(states=="1"){
                        re="审核通过";
                    }else{
                        re="审核不通过";
                    }
                    var temp={
                        id:result[i].id,
                        issuer:result[i].issuer,
                        appname:result[i].appname,
                        issuetime:result[i].issuetime,
                        function:result[i].function,
                        displaypath:result[i].displaypath,
                        issueadress:result[i].issueadress,
                        email:result[i].email,
                        phonenumber:result[i].phonenumber,
                        states:re,
                    }
                    results.push(temp);
                }
                for(var j=0;j<results.length;j++){
                    $('#problemsTable').bootstrapTable('append', results[j]);
                }
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {//输出错误信息
            alert('读取APP申请信息失败，请稍后重试');
        }
    });
}



//管理员页面APP信息编辑
window.infoEvents ={
    'click .edit': function (e, value, row, index) {
        var appname=row.appname;
        var issuetime=row.issuetime;
        var function1=row.function;
        var displaypath=row.displaypath;
        var issueadress=row.issueadress;
        var email=row.email;
        var phonenumber=row.phonenumber;
        $('#appInfoEditModal').modal('show');
        $("#name").val(appname);
        $("#issueTime").val(issuetime);
        $("#functionInfo").val(function1);
        $("#displaypath").val(displaypath);
        $("#issuerAdress").val(issueadress);
        $("#issuerEmail").val(email);
        $("#issuerPhone").val(phonenumber);

        $("#appInfoEditBtn").unbind("click");
        $("#appInfoEditBtn").click(index,editAPPInfoBtn);

        $("#appInfoEditBtn").css("display","inline");

    }
};
function checkAPPInfoInput() {
    if ($("#name").val().trim()=="") {
        alert("项目名称不能为空！");
        return false;
    }
    if ($("#issueTime").val().trim()=="") {
        alert("发布时间不能为空！");
        return false;
    }
    if ($("#functionInfo").val().trim()=="") {
        alert("功能描述不能为空！");
        return false;
    }
    if ($("#displaypath").val().trim()=="") {
        alert("访问链接不能为空！");
        return false;
    }
    if ($("#issuerAdress").val().trim()=="") {
        alert("发布者单位不能为空！");
        return false;
    }
    if ($("#issuerEmail").val().trim()=="") {
        alert("邮箱不能为空！");
        return false;
    }
    if ($("#issuerPhone").val().trim()=="") {
        alert("联系电话不能为空！");
        return false;
    }
    return true;
}
function editAPPInfoBtn(event) {
    if (userInfos.length>0){
        if (checkAPPInfoInput()){
            var temId=$('#problemsTable').bootstrapTable('getData')[event.data].id;
            var rowData={
                id:temId,
                appname:$("#name").val(),
                issuetime:$("#issueTime").val(),
                function:$("#functionInfo").val(),
                displaypath:$("#displaypath").val(),
                issueadress:$("#issuerAdress").val(),
                email:$("#issuerEmail").val(),
                phonenumber:$("#issuerPhone").val()
            };
            $.ajax({
                type: "post",
                url: "apply/update",
                data: rowData,
                success: function (data) {
                    if (data){
                        $("#appInfoEditModal").modal("hide");
                        getAllAPPInfo();
                    } else{
                        alert("项目名称已存在，请重新编辑！")
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {//输出错误信息
                    alert('更新失败，请稍后重试');
                }
            });
        }
    }
}